home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MPW_TOOL
/
TOOLS
/
TOOLS_WI
/
ICON_8
/
CALLING_
/
EXTNAME.C
< prev
next >
Wrap
Text File
|
1990-04-08
|
2KB
|
64 lines
/*
* Example of calling C functions by their names. Here it's just
* chdir (change directory) or getwd (get path of current working directory).
*/
#include "../h/config.h"
#include "../h/rt.h"
#include "rproto.h"
struct descrip retval; /* for returned value */
dptr extcall(dargv, argc, ip)
dptr dargv;
int argc;
int *ip;
{
int len, retcode;
char sbuf1[MaxCvtLen]; /* for conversion on non-strings */
char sbuf2[MaxCvtLen]; /* for C-style string */
int chdir(), getwd();
*ip = -1; /* anticipate error-free execution */
if (cvstr(dargv, sbuf1) == CvtFail) { /* 1st argument must be a string */
*ip = 103; /* "string expected" error number */
return dargv; /* return offending value */
}
if (strncmp("chdir", StrLoc(*dargv), StrLen(*dargv)) == 0) {
if (argc < 2) { /* must be a 2nd argument */
*ip = 103; /* no error number fits, really */
return NULL; /* no offedning value */
}
dargv++; /* get to next argument */
if (cvstr(dargv, sbuf1) == CvtFail) { /* 2nd argument must be a string */
*ip = 103; /* "string expected" error number */
return dargv; /* return offending value */
}
qtos(dargv,sbuf2); /* get C-style string in sbuf2 */
retcode = chdir(sbuf2); /* try to change directory */
if (retcode == -1) /* see if chdir failed */
return (dptr)NULL; /* signal failure */
return &zerodesc; /* not a very useful result */
}
else if (strncmp("getwd", StrLoc(*dargv), StrLen(*dargv)) == 0) {
dargv++; /* get to next argument */
retcode = getwd(sbuf2); /* get current working directory */
if (retcode == 0) /* see if getwd failed */
return NULL; /* signal failure */
len = strlen(sbuf2); /* length of resulting string */
if (strreq(len) == Error) { /* need to allocate a copy of result */
*ip = 0; /* zero since code is set elsewhere */
return (dptr)NULL; /* no offending value */
}
StrLoc(retval) = alcstr(sbuf2,len); /* allocate and copy the string */
StrLen(retval) = len;
return &retval; /* return a pointer to the qualifier */
}
else {
*ip = 216; /* name is not one of those supported here */
return dargv; /* return pointer to offending value */
}
}